package com.youdianstar.app.common.util

import android.content.Context
import android.content.res.Configuration
import android.content.res.Resources
import android.os.Build
import android.os.LocaleList
import android.util.Log
import com.youdianstar.app.common.Language
import java.util.*


/**
 * File: LanguageUtils
 * Author: 82149 Create: 2022/5/26 16:55
 * Changes (from 2022/5/26)
 * --------------------------------------------------
 * description:
 * ---------------------------------------------------
 *
 */
class LanguageUtils {

    companion object{
        /**
         * 获取系统语言
         */
        private fun getSystemLanguageCode():String{
            val locale = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                LocaleList.getDefault().get(0)
            } else {
                Locale.getDefault()
            }
            return locale.language.lowercase(Locale.ROOT)
        }

        /**
         * 获取默认语言 判断系统语言 与 app 配置的系统
         */
        fun getDefaultLanguageCode():String{
            // 默认返回越南语
            return Language.VI
            
            // 原来的系统语言判断逻辑（已注释）
            /*
            when(getSystemLanguageCode()){
                Locale(Language.ES).language.lowercase() ->{
                    return Language.ES
                }
                Locale(Language.CHINESE.language.lowercase() ->{
                    return Language.ZH
                }
                Locale(Language.VI).language.lowercase() ->{
                    return Language.VI
                }
                else ->{
                    return Language.EN
                }
            }
            */
        }

        /**
         * 获取选择的语言设置
         *
         * @param local
         * @return
         */
        private fun getSetLanguageLocale(local: String): Locale{
            if (StrUtil.isEmpty(local)) {
                return Locale.GERMAN
            }
            return when (local) {
                Language.ES -> Locale(Language.ES)
                Language.ZH -> Locale.CHINESE
                Language.EN -> Locale.ENGLISH
                Language.VI -> Locale(Language.VI)
                else -> Locale.GERMAN
            }
        }

        fun getLanguageLocale(): Locale{
            val language: String = LanguageConfig.instance.getLanguageCode()
            return when (language) {
                Language.ES -> Locale(Language.ES)
                Language.ZH -> Locale.CHINESE
                Language.EN -> Locale.ENGLISH
                Language.VI -> Locale(Language.VI)
                else -> Locale.GERMAN
            }
        }


        /**
         * 更新资源 Context
         * BaseActivity
         */
        fun createAttachContext(context: Context, language: String): Context? {
            val resources = context.resources
            val locale: Locale = getSetLanguageLocale(language)
            val configuration = resources.configuration
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                Log.i("Language","---setLan attachContext = "+locale.language)
                val localeList = LocaleList(locale)
                LocaleList.setDefault(localeList)
                Locale.setDefault(locale)
                configuration.setLocale(locale)
                configuration.setLocales(localeList)
                return context.createConfigurationContext(configuration)
            } else {
                configuration.setLocale(locale)
                resources.updateConfiguration(configuration,resources.displayMetrics)
                return context
            }
        }

        /**
         * 语言配置
         * Y
         */
        fun changeLocalLanguage(context : Context, languageSet: String? = null) {
            val language = languageSet ?: LanguageConfig.instance.getLanguageCode()
            val res: Resources = context.resources
            val conf: Configuration = res.configuration
            val locale = getSetLanguageLocale(language)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                Log.i("Language","---setLan Language = "+locale.language)
                val localeList = LocaleList(locale)
                LocaleList.setDefault(localeList)
                Locale.setDefault(locale)
                conf.setLocales(localeList)
                conf.setLocale(locale)
                context.createConfigurationContext(conf)
                res.updateConfiguration(conf, res.displayMetrics)
            } else {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
                    conf.setLocale(locale)
                }else{
                    conf.locale = locale
                }
                res.updateConfiguration(conf, res.displayMetrics)
            }
        }
    }

}